--
-- Author: ping.wang
-- Date: 2019-04-30 16:15:32
--
-- NewRankController
--

local NewRankView = import("..views.NewRankView")
local NewRankGuildDetailView = import("..views.NewRankGuildDetailView")

local fileName = string.getModeName(...)
local ClassRef = class(fileName, app.mvc.ControllerBase)

function ClassRef:ctor(data)
    self.super.ctor(self, data)
end

function ClassRef:init()
    self._model = app:getInst("NewRankModel")
end

function ClassRef:removeListeners()
    self.super.removeListeners(self)
end

function ClassRef:onEvent(eventName, data)
end

function ClassRef:listEvent()
    return {}
end


function ClassRef:openView(data)
    if nil == self._view then
        self._view = NewRankView.new(app.mvc.VIEWTYPE_BLUR)
        self._view:onCloseView(handler(self, self.closeView))
        self._view:onClearView(handler(self, self.clearView))
        self._view:onOperate(handler(self, self.onOperate))
    end
    self:addChild(self._view, data)

    self._view:show()
    self._view:initLeftList(self._model:getRankMenuData(), data)
end
function ClassRef:clearView()
    if self._view then
        self._view = nil
    end
end


function ClassRef:showGuildDetailView(data)
    if nil == self._guildDetailView then
        self._guildDetailView = NewRankGuildDetailView.new(app.mvc.VIEWTYPE_BLUR, data)
        self._guildDetailView:onCloseView(handler(self, self.closeGuildDetailView))
        self._guildDetailView:onClearView(handler(self, self.clearGuildDetailView))
        self._guildDetailView:onOperate(handler(self, self.onOperate))
    end
    self:addChild(self._guildDetailView)

    self._guildDetailView:show()
end

function ClassRef:clearGuildDetailView()
    if self._guildDetailView then
        self._guildDetailView = nil
    end
end

function ClassRef:closeGuildDetailView()
    if self._guildDetailView then
        self:removeChild(self._guildDetailView)
    end
end

-------------------------------------------------------


-------------------------------------------------------
function ClassRef:onOperate(op, data)
    print("NewRankController:onOperate op = " .. op)
    if op == "showRankInfo" then
        if cc.isAlive(self._model) then
            self._model:requestGuildRankWithRankConfig(data)
        end
    elseif op == "showGuildDetail" then
        self:showGuildDetailView(data)
    elseif op == "showMyGuildDetail" then
        --显示我的公会详情
        local guildModel = app:getInst("NewguildModel")
        if cc.isAlive(guildModel) then
            local myGuildInfo = guildModel:getMyGuildInfo()
            if myGuildInfo then
                self:showGuildDetailView(myGuildInfo)
            end
        end
    elseif op == "applyGuild" then
        local guildModel = app:getInst("NewguildModel")
        if cc.isAlive(guildModel) then
            guildModel:reqApplyGuild(data)
        end
    end
end

--根据服务器数据来刷新公会排行榜界面
-- rankData = {rankInfos={ {paiming = k, guildInfo = guildVo}},myGuildPaiming=number}
function ClassRef:refreshGuildRankView(rankData)
    if cc.isAlive(self._view) then
        self._view:refreshView(rankData)
    end
end

return ClassRef
